<?php
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_COST_TVSP_MATER.php' );
require_once( '../system/mdao/Class_M_COST_TVSP_MELEC.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/login.inc.php' );

Class_PWD::valid_auth(AOKI_AUTH_COST,$login['staff_auth']);

$dao       =  new Class_M_COST_TVSP_MATER;
$daoE =  new Class_M_COST_TVSP_MELEC( $dao->get_db() );
if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	$phase = 'input';

	if( isset($_GET['id']) && $_GET['id'] != '' ){
		$forms  = $dao->get( $_GET['id'] );
		$forms["id"] = $_GET['id'];
		
		
		$dbarray = $dao->getFormElectric($_GET['id'] );
		//データ整形
		foreach ($dbarray as $k=>$v) {
			$elecchk = empty($v["electricid"]) ? 0:1;
			$forms["mutirow"] [] = array( "electricid"=> $v["id"], "electric_name" => $v["electric_name"], "elecchk" => $elecchk );
			
		}
	}else{
		//$forms['auth'] = 0;
	}

}else{
	$forms = $_POST;
	if( $forms['mode'] == 'input' ){
		$err     = new Class_ERROR;
		$chk     = $dao->get_checks();
		
		foreach ($chk as $kcol => $chkval) {
			$err_mes[$kcol]  = $err->check( $forms[$kcol], $chkval );
		}
		//elecchk[]
		if (isset($forms["electricid"])) {
			foreach ($forms["electricid"] as $k=>$v) {
				$elecchk = in_array($v, $forms["elecchk"]) ? 1:0;
				$forms["mutirow"] [] = array( "electricid"=> $v ,"electric_name" => $forms["electric_name"][$k], "elecchk" => $elecchk );
			}
		} 
		
		//	print_r($forms["mutirow"]);
		//	exit;
			
		if ( $err->clear ){
			$cols     = $dao->get_cols();
			foreach($cols as $kcol => $val) {
				$values[$kcol] = $forms[$kcol] ;
			}
			
			//$values["media_cd"]    =  Class_M_OTHER::do_media($values["media_name"]);
			//$values["sent_date"]   =  Class_PWD::date_to_int8 ($values['sent_date']) ;
			
			//桁数チェック
			$maxlens = $dao->get_maxlens();
			foreach ( $maxlens as $k => $v ) {
				$err_mes[$k] =$err->check_size($values[$k], $v);
			}
		}
		

		if( $err->clear ){
			if( $forms['id'] == '' ){
				if ($dao->add0($values)) {
					$forms['id'] = $id;
					$smarty->assign('message', '登録が完了しました。');
					
				} else {
					$smarty->assign('message', '登録が失敗しました。');
				}
			}else{
				
				
				if ($dao->edit( $forms['id'], $values )) {
					$smarty->assign('message', '更新が完了しました。');
				} else {
					$smarty->assign('message', '更新が失敗しました。');
				}
				try {	
					$daoE->begin_trans();
					
					$executeflg = TRUE;
					//$disp_ng = $forms["disp_ng"]?$forms["disp_ng"]:0;
					$wherearr[] = sprintf(" materid =  '%s' ", $forms['id'] );
					$dbarray = $daoE->search($wherearr );
					//削除.更新のデータ作成
					foreach($dbarray as $k=>$v) {
						$delflg = TRUE;
						
						$val_shop_per  = "0";
						foreach($forms["elecchk"] as $kk=>$vv) {
							if ($v["electricid"] == $vv ) {
								$delflg = FALSE;
								break;
							}
						}
						if ($delflg){
							//$datadel[]  = $v["shop_cd"];
							if ($daoE->remove($v["id"])) {
							} else {
								$executeflg = FALSE;
							}
						} else {
							/*
							//$dataedit[] = $v["shop_cd"];
							$values = array();
							
							if ($daoE->edit( $v["id"] ,$values)) {
							} else {
								$executeflg = FALSE;
							}
							*/
						}
					}
						
						//追加のデータ作成
						foreach($forms["elecchk"] as $k=>$v) {
							$taddflg = TRUE;
							foreach($dbarray as $kk=>$vv) {
								if ($v == $vv["electricid"] ) {
									$taddflg = FALSE;
									break;
								}
							}
							if ($taddflg){
								//$dataadd[] = $v["shop_cd"];
								$values = array();
								$values["materid"] 		= $forms['id'];
								$values["electricid"]   = $v["electricid"] ;
								
								
								if ($daoE->addnotime($values)) {
								} else {
									$executeflg = FALSE;
								}
							}
						}
						if  ($executeflg) {
							$daoE->commit_trans();
							$smarty->assign('message', '更新が完了しました。');
						} else {
							$daoE->rollback_trans();
							$smarty->assign('message', '更新が失敗しました。');
						}
					} catch (Exception $e) {
						$daoE->rollback_trans();
						$smarty->assign('message', 'ＤＢエラーで失敗しました。');
					}
				$id = $forms['id'];
			}
			$phase = 'complete';
		}else{
			$phase = 'input';
		}
	}
}


if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}
$smarty->assign('year4',  Class_PWD::get_years());
if( isset( $forms ) ) $smarty->assign('forms', $forms);
if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);

if ($phase != 'complete') {
	
}
/*
$electrics = Class_M_OTHER::get_electric_array(PLEASE_SELECT_ARR);
$smarty->assign('electrics', $electrics );
*/

$smarty->assign('phase', $phase );
$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );

$smarty->display('mcost_tvsp_mater_form.html');
?>